from java.awt import BorderLayout
from javax.swing import JLabel, JScrollPane
from org.memesis.pandorasbook.ui import PandoraTextPane

class SchmuckTextSender(object):
	def __init__(self, socket_id):
		self.socket_id = socket_id
		
	def receive_line(self, s):
		socket_manager.send(self.socket_id, s)

class SchmuckFrame(object):
	def __init__(self):
		self.commands = SchmuckCommandManager(self)

		# self.out has to exist here
		self.output = SchmuckOutputComponent()

		# TODO: refactor this out
		self.socket_id = socket_manager.connect(applet.getProperty("host"), int(applet.getProperty("port")), self)
		sender = SchmuckTextSender(self.socket_id)
		frame.setTitle("Connected to " + applet.getProperty("sitename"))
		
		self.input = SchmuckInputComponent(self.commands, sender)

		# Add some default keybindings
		self.input.bind("PAGE_UP",   "prev-in-history")
		self.input.bind("PAGE_DOWN", "next-in-history")
		self.input.bind("control P", "prev-in-history")
		self.input.bind("control N", "next-in-history")

		frame.setLayout(BorderLayout())
		frame.add(self.output, BorderLayout.CENTER)
		frame.add(self.input, BorderLayout.SOUTH)

		self.maximize_frame()

	def maximize_frame(self):
		frame.reshape(0, 0, frame.getDesktopPane().getWidth(), frame.getDesktopPane().getHeight());

	def receive_line(self, socket_id, line):
		self.output.receive_line(socket_id, line)

	def socket_closed(self, socket_id):
		self.output.receive_line(socket_id, "*** Disconnected ***")
		frame.setTitle("Disconnected")

f = SchmuckFrame()